<template>
  <div class="app-container">
    <el-row>
      <el-col :span="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="姓名" prop="name" >
            <el-input
              v-model="queryParams.name"
              placeholder="请输入姓名"
              clearable
              style="width:150px"
            />
          </el-form-item>
          <el-form-item label="所属机构" prop="belongUnit">
            <el-input
              v-model="queryParams.belongUnit"
              placeholder="请输入所属机构"
              clearable
            />
          </el-form-item>
          <el-form-item label="讲师类型" prop="teacherType">
            <el-select v-model="queryParams.teacherType" placeholder="请选择讲师类型" style="width:150px">
              <el-option
                v-for="dict in dict.type.teacher_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select >
          </el-form-item>
          <el-button type="primary" @click="handleQuery">查询</el-button>
          <el-button @click="onHandleQuery">重置</el-button>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
            >导出</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table     :cell-style="{ padding: '0px' }" height="450" border  v-loading="loading" :data="teacherInfoList" highlight-current-row>
          <el-table-column
            label="序"
            type="index"
            :index="indexMethod" align="center" :width="40"/>
          <el-table-column label="创建时间" align="center" prop="creationTime" :width="85"/>
          <el-table-column label="创建时间" align="center" prop="creationTime" :show-overflow-tooltip="true" width="70"/>
          <el-table-column label="创建人" align="center" prop="userName" :show-overflow-tooltip="true" width="70"/>
          <el-table-column label="姓名" align="center" prop="name" :show-overflow-tooltip="true" :width="60"/>
          <el-table-column label="类型" align="center" prop="teacherType" :show-overflow-tooltip="true"  :width="70">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.teacher_type" :value="scope.row.teacherType"/>
            </template>
          </el-table-column>
          <el-table-column label="联系方式" align="center" prop="phone"  :width="100"/>
          <el-table-column label="所属机构" align="center" prop="belongUnit" :show-overflow-tooltip="true" :width="210"/>
          <el-table-column label="主要专业" align="center" prop="speciality"  :formatter="formatHtmlContent" :show-overflow-tooltip="true"/>
          <el-table-column label="主讲课程" align="center" prop="course"  :show-overflow-tooltip="true" :formatter="formatHtmlContent"/>
          <el-table-column label="荣誉资质" align="center" prop="qualification"  :show-overflow-tooltip="true" :formatter="formatHtmlContent" />
          <el-table-column label="工作经历" align="center" prop="workExperience"  :show-overflow-tooltip="true" :width="100" :formatter="formatHtmlContent"/>
          <el-table-column label="照片" align="center" prop="uploadFile" >
            <template slot-scope="scope">
              <el-button @click.stop="handlePictureCardPreview(scope.row.uploadFile)" size="small"
                         type="text">附件</el-button>
            </template>
          </el-table-column>
          <el-table-column label="状态" align="center" prop="status"  >
            <template slot-scope="scope">
              <dict-tag :options="dict.type.teacher_status" :value="scope.row.status"/>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="70px"  fixed="right">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row,scope.$index)"
              ></el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-select"
                @click="handleSelect(scope.row,scope.$index)"
              ></el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-s-custom"
                @click="handleSnap(scope.row)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
    <!-- 添加或修改师资管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body :rules="rules" :before-close="handleClose">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" :inline="true">
        <div v-if="addSign===1">
          <el-descriptions title="">
            <el-descriptions-item label="序号">
              <el-tag size="medium">{{form.index}}</el-tag>
            </el-descriptions-item>
          </el-descriptions>
        </div>
       <el-row>
         <el-form-item label="姓名" prop="name">
           <el-input v-model="form.name" placeholder="请输入姓名" style="width: 202.5px"/>
         </el-form-item>
         <el-form-item label="所属机构" prop="belongUnit">
           <el-input v-model="form.belongUnit" placeholder="请输入所属机构" style="width: 202.5px"/>
         </el-form-item>
         <el-form-item label="讲师类型" prop="teacherType">
           <el-select v-model="form.teacherType" placeholder="请选择讲师类型" style="width: 202.5px" >
             <el-option
               v-for="dict in dict.type.teacher_type"
               :key="dict.value"
               :label="dict.label"
               :value="dict.value"
             ></el-option>
           </el-select>
         </el-form-item>
         <el-form-item label="联系电话" prop="phone">
           <el-input v-model="form.phone" placeholder="请输入联系电话" style="width: 202.5px"/>
         </el-form-item>
         <el-form-item label="状态" prop="status">
           <el-select v-model="form.status" placeholder="请选择状态" style="width: 202.5px">
             <el-option
               v-for="dict in dict.type.teacher_status"
               :key="dict.value"
               :label="dict.label"
               :value="dict.value"
             ></el-option>
           </el-select>
         </el-form-item>
         <el-form-item label="工作经历" prop="workExperience" style="width: 100%">
           <!--           <el-input v-model="form.workExperience" type="textarea" placeholder="请输入内容" rows="5px" style="width: 500px"/>-->
           <quill-editor
             class="ql-editor"
             v-model="form.workExperience"
             ref="myQuillEditor"
             :options="editorOption"
             @blur="onEditorBlur($event)"
             @focus="onEditorFocus($event)"
             @change="onEditorChange($event)">
           </quill-editor>
         </el-form-item>
         <el-form-item label="主要特长" prop="speciality" style="width: 100%">
<!--                     <el-input v-model="form.speciality" type="textarea" placeholder="请输入内容" rows="5px" style="width: 500px"/>-->
           <quill-editor
             class="ql-editor"
             v-model="form.speciality"
             ref="myQuillEditor"
             :options="editorOption"
             @blur="onEditorBlur($event)"
             @focus="onEditorFocus($event)"
             @change="onEditorChange($event)">
           </quill-editor>
         </el-form-item>
         <el-form-item label="主讲课程" prop="course" style="width: 100%">
<!--           <el-input v-model="form.course" type="textarea" placeholder="请输入内容" rows="5px" style="width: 500px"/>-->
           <quill-editor
             class="ql-editor"
             v-model="form.course"
             ref="myQuillEditor"
             :options="editorOption"
             @blur="onEditorBlur($event)"
             @focus="onEditorFocus($event)"
             @change="onEditorChange($event)">
           </quill-editor>
         </el-form-item>
         <el-form-item label="服务部分企业" prop="servicePrices" style="width: 100%" label-width="110px">
           <!--           <el-input v-model="form.servicePrices" type="textarea" placeholder="请输入内容" rows="5px" style="width: 500px"/>-->
           <quill-editor
             class="ql-editor"
             v-model="form.servicePrices"
             ref="myQuillEditor"
             :options="editorOption"
             @blur="onEditorBlur($event)"
             @focus="onEditorFocus($event)"
             @change="onEditorChange($event)">
           </quill-editor>
         </el-form-item>
         <el-form-item label="取得资质或荣誉" prop="qualification" style="width: 100%" label-width="110px">
<!--           <el-input v-model="form.qualification" type="textarea" placeholder="请输入内容" rows="5px" style="width: 500px"/>-->
           <quill-editor
             class="ql-editor"
             v-model="form.qualification"
             ref="myQuillEditor"
             :options="editorOption"
             @blur="onEditorBlur($event)"
             @focus="onEditorFocus($event)"
             @change="onEditorChange($event)">
           </quill-editor>
         </el-form-item>
         <el-form-item label="创建时间" prop="creationTime">
           <el-date-picker clearable
                           v-model="form.creationTime"
                           type="date"
                           value-format="yyyy-MM-dd"
                           placeholder="请选择创建时间"
                           style="width: 202.5px"
                           disabled="true">
           </el-date-picker>
         </el-form-item>
         <el-form-item label="创建人" prop="userName">
           <el-input v-model="form.userName" placeholder="请输入所属机构"  disabled="true"/>
         </el-form-item>

         <el-form-item label="照片" prop="teacherFile" style="width: 100%" >
           <file-view  v-if="show"  :limit="1" :type="typeSelect" :businessId="form.uploadFile" :businessCode="businessCode" @getFile="getFile"  ></file-view>
         </el-form-item>
       </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" >
        <el-button type="primary" @click="submitForm" v-if="typeSelect!==2">确 定</el-button>
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="快照"
      :visible.sync="dialogVisible"
      width="50%"
      :before-close="closeSnapShot">
      <snapshot :teacherId="teacherId" v-if="dialogVisible"></snapshot>
    </el-dialog>
    <!--   履历 附件展示对话框-->
    <el-dialog title="查看附件" :visible.sync="openFileList" width="500px" append-to-body v-if="openFileList">
      <UploadFile :upload-id="uploadId" :business-code="businessCode" ></UploadFile>
    </el-dialog>
  </div>
</template>

<script>
import MainType from "@/components/MainType/index"
import { listTeacherInfo, getTeacherInfo, delTeacherInfo, addTeacherInfo, updateTeacherInfo } from "@/api/procure/teacherInfo";
import dia from "../../enterprise/seas/dialog.vue";
import FileView from "@/components/FileView/index"
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {listType} from "@/api/maintype/type";
import Snapshot from "./snapshot.vue";
import UploadFile from "../../upload/index.vue";
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor' //引入插件
export default {
  name: "TeacherInfo",
  dicts: ['teacher_status','teacher_type'],
  components: {UploadFile, MainType,Treeselect,FileView,Snapshot,quillEditor},
  data() {
    return {
      //快照对话框标志
      dialogVisible:false,
      //教师id
      teacherId:null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      ImgDig:false,
      dialogImageUrl:null,
      // 非多个禁用
      multiple: true,
      //附件对话框
      openFileList:false,
      uploadId:null,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 师资管理表格数据
      teacherInfoList: [],
      show:false,
      typeSelect:0,
      businessCode:'teacherInfo',
      // 弹出层标题
      title: "",
      //主分类
      options:null,
      // 是否显示弹出层
      open: false,
      openselect:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 15,
        name: null,
        belongUnit: null,
        teacherType: null,
        classificationId:null,
      },
      // 富文本功能配置
      editorOption:{
        modules:{
          toolbar:[
            ['bold', 'italic', 'underline', 'strike'],    //加粗，斜体，下划线，删除线
            ['blockquote', 'code-block'],     //引用，代码块
            [{ 'header': 1 }, { 'header': 2 }],        // 标题，键值对的形式；1、2表示字体大小
            [{ 'list': 'ordered'}, { 'list': 'bullet' }],     //列表
            [{ 'script': 'sub'}, { 'script': 'super' }],   // 上下标
            [{ 'indent': '-1'}, { 'indent': '+1' }],     // 缩进
            [{ 'direction': 'rtl' }],             // 文本方向
            [{ 'size': ['small', false, 'large', 'huge'] }], // 字体大小
            [{ 'header': [1, 2, 3, 4, 5, 6, false] }],     //几级标题
            [{ 'color': [] }, { 'background': [] }],     // 字体颜色，字体背景颜色
            [{ 'font': [] }],     //字体
            [{ 'align': [] }],    //对齐方式
            ['clean'],    //清除字体样式
          ]
        }
      },
      typeCode:'teacherInfo',
      addSign:0,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' },
          { min: 2, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        belongUnit: [
          { required: true, message: '请输入所属单位', trigger: 'blur' }
        ],
        teacherType: [
          { required: true, message: '请输入讲师类型', trigger: 'change' }
        ],
        phone: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 5, max:11, message: '请输入正确的电话号码', trigger: 'blur' }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.listType();
  },
  methods: {
    /** 查询师资管理列表 */
    getList() {
      this.loading = true;
      listTeacherInfo(this.queryParams).then(response => {
        this.teacherInfoList = response.rows;
        console.log(this.teacherInfoList)
        this.total = response.total;
        this.loading = false;
      });
    },
    // 事件方法
    onEditorBlur(e){
      console.log(e, '失去焦点事件');
    },
    onEditorFocus(e){
      console.log(e, '获得焦点事件');
    },
    onEditorChange(e){
      console.log(e, '内容改变事件');
    },
    //树点击事件
    getNode(data){
      console.log(data)
      if(data.parentId!='00000000-0000-0000-0000-000000000000'){
        this.queryParams.classificationId=data.id
      }else {
        this.queryParams.classificationId=null;
      }
      this.getList()
    },
    // 取消按钮
    cancel() {
      this.typeSelect=0;
      this.show=false;
      this.open = false;
      this.reset();
    },
    handleClose(){
      this.cancel();
    },
    formatHtmlContent(row, column) {
      // 使用 DOMParser 将 HTML 字符串转换为 DOM 对象
      const parser = new DOMParser();
      const doc = parser.parseFromString(row[column.property] || '', 'text/html');
      var textContent = doc.body.textContent;
      const title = textContent.length > 50 ? textContent.substring(0, 80) + '...' : textContent;
      // 获取文本内容并返回
      return title || '';
    },
    getFile(data){
      console.log(JSON.stringify(data))
      console.log(data[0].id)
      this.form.teacherFile=data[0].id;
    },
    //获取树节点数据
    listType(){
      listType({typeCode:'teacherInfo'}).then(res=>{
        this.options=res.data;
      })
    },
    //做字符显示限制
    stateFormat(row, column, cellValue) {
      if (!cellValue) return ''
      if (cellValue.length > 10) {       //最长固定显示10个字符
        return cellValue.slice(0, 10) + '...'
      }
      return cellValue
    },
//序号展示
    indexMethod(index){
      return (this.queryParams.pageNum-1)*this.queryParams.pageSize +index+1;
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        belongUnit: null,
        teacherInfo: null,
        teacherType:null,
        speciality: null,
        course: null,
        qualification: null,
        servicePrices: null,
        phone: null,
        operateId: null,
        deparmentId: null,
        deparmentName: null,
        organizationId: null,
        organizationName: null,
        uploadFile: null,
        creationTime: null,
        operateTime: null,
        //用于校验照片是否上传,
        teacherFile:null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /**
     * 重置
     */
    onHandleQuery(){
      this.queryParams.name=null;
        this.queryParams.belongUnit=null;
        this.queryParams.teacherType=null;
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
     this.reset();
      this.typeSelect=0;
      this.form.creationTime=new Date();
      this.form.userName=this.$store.state.user.name;
      if (this.queryParams.classificationId===null){
        this.form.classificationId='650c44e1-b575-4b62-9d54-1ba9d1e8abb7'
      }else {
        this.form.classificationId=this.queryParams.classificationId;
      }
      this.addSign=0;
      this.form.uploadFile=new Date().getTime();
      this.listType();
      this.show=true;
      this.open = true;
      this.title = "添加师资管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row,index) {
      this.reset();
      const id = row.id || this.ids
      getTeacherInfo(id).then(response => {
        // this.listType();
        this.form = response.data;
        this.addSign=1;
        this.form.index=index+1;
        //给照片标志附上默认值
        this.form.teacherFile=1;
        this.typeSelect=1;
        console.log(this.typeSelect)
        console.log(this.form)
        if (this.form.uploadFile===null ){
           this.form.uploadFile=new  Date().getTime();
        }
        this.listType();
        this.show=true;
        this.open = true;
        this.title = "修改师资管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      console.log(this.form)
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            console.log(this.form)
            updateTeacherInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.typeSelect=0;
              this.open = false;
              this.show=false;
              this.getList();
            });
          } else {
            addTeacherInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.typeSelect=0;
              this.open = false;
              this.show=false;
              this.getList();
            });
          }
        }
      });
    },
    //查看按钮
    handleSelect(row,index){
      this.reset();
      const id = row.id || this.ids
      getTeacherInfo(id).then(response => {
        this.form = response.data;
        this.openselect = true;
        this.title = "查看师资管理";
        this.addSign=1;
        this.form.index=index+1;
        this.typeSelect=2;
        this.show=true;
        this.open = true;
      });
    },
    //快照
    handleSnap(row){
      this.dialogVisible=true;
      this.teacherId=row.id;
    },
    closeSnapShot(){
      this.dialogVisible=false;
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('procure/teacherInfo/export', {
        ...this.queryParams
      }, `teacherInfo_${new Date().getTime()}.xlsx`)
    },
    handlePictureCardPreview(id) {
      this.uploadId=id;
      this.openFileList = true;
    },
  }
};
</script>
